<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script src="./js/jquery-1.7.2.min.js"></script>
</head>
<style>
.fontClass{
	font-size : 20px;
	font-weight : blod;
	letter-spacing : 8px;
}

div.line{
border-top:2px solid black;
margin:0px;
width:100%;
}

</style>
<script>
window. addEventListener ('message', function(event) { 
	//event.data获取传过来的数据
	if(event.data.type == "print"){
		printTest();
	}else if(event.data.type == "test"){
		generate(event.data.testData);
	}
});

function printTest(){
  window.print(); 
}

//判断计算题目是否重复
//只是字符串比较 1+2 和2+1 为false
var maxCacheCount = 60; //多少个以内不重复
var resultCache = []
function isExist(testItem){
	var result = false;
	for(var i = 0;i<resultCache.length;i++){
		if(resultCache[i].str == testItem.str){
			console.log(testItem);
			result = true;
			break;
		}
	}
	
	if(!result){
		resultCache.push(testItem);
		if(resultCache.length>maxCacheCount){
			resultCache.unshift();
		}
	}
	
	return result;
}

//生成计算题目
function generate(options){
	$("#testList").empty();
	resultCache = [];
	
	//console.log(options);
	
	if(options.totalWeight >0){
		var result = [];
		var sumCount = options.total;
		
		var generatCount = 0;
		
		var itemNumberArray=[];
		jQuery.each(options.item,function(i,n){
			itemNumberArray.push(0);
		});
		
		
		while(sumCount >0 ){
			//在总的权重下面,得到一个随机数
			//通过随机数,得到当前需要整哪个计算
			
			var itemWeightArray = [];
			var calTime = 0;
			for(var i = 0 ; i< options.item.length;i++){
				calTime = Math.max(options.item[i].value,calTime);	
			}
			for(var i = 0 ; i<calTime;i++){
				for(var j = 0 ; j< options.item.length;j++){
					if(options.item[j].value>(i)){
						itemWeightArray.push(j);
					}
				}
			}
			
			//console.log(itemWeightArray);
			
			jQuery.each(itemWeightArray,function(i,n){
				var targetScript = options.item[n].method+"();";
				//console.log(targetScript);
				var testItem = eval(targetScript);
				while(!testItem  || (isExist(testItem) && generatCount<options.total*10)){
					testItem = eval(targetScript);
					generatCount++;
				}
				//判断是否重复
				//增加生成次数超过题目总数的十倍,说明选择的项目中,题目一定有重复,跳过isExist的判断,直接生成,否则会死循环
				//if(!isExist(testItem) || generatCount>options.total*10){
					result.push(testItem);
					sumCount --;	
					itemNumberArray[n] = itemNumberArray[n]+1;
				//}
				
				
			});
		}
		
		//console.log(itemNumberArray);
		
		//开始渲染题目表格
		var table = $("<table></table>").addClass("testTable").attr("width","100%");
		var colCount = 0;
		var lastTr;
		$.each(result,function(i,n){
			colCount = i % options.colNum;
			if(colCount == 0){
				lastTr = $("<tr></tr>").addClass("testTableRow");
				table.append(lastTr);
			}
			
			var col = showTestItem(n);
			var td = $("<td></td>");
			td.append(col);
			lastTr.append(td);
		});
		
		$("#testList").append(table);
	}
}

/**
*	生成单个的计算题目
*/
function showTestItem(testItem){
	
	var testTable = $("<table></table>").addClass("shuTestItem").attr("width","150px").attr("border","0");
	var tr0 = $("<tr></tr>").attr("height" , "10px");
	testTable.append(tr0);
	var tr = $("<tr></tr>");
	tr.append($("<td></td>").attr("width" , "20%"));
	tr.append($("<td></td>").attr("width","14px"));
	testTable.append(tr);
	var tr2 = $("<tr></tr>");
	tr2.append($("<td></td>").attr("align" , "right").addClass("fontClass").text(testItem.right));
	tr2.append($("<td></td>").addClass("chu"));
	tr2.append($("<td></td>").addClass("fontClass").attr("align","left").append($("<div></div>").addClass("line").text(testItem.left)));
	testTable.append(tr2);
	
	var tr4 = $("<tr></tr>").attr("height","120px");
	testTable.append(tr4);
	//$("#listTestList").append(testTable);
	return testTable;
}

//--------------------------------------计算方法

//100以内整除10
function innerHundredChu(){
	var one , two , result;
	two = 2+Math.round(Math.random() * 7);
	one = 50 + Math.round(Math.random() * 49);
	
	result = Math.round(one/two);
	
	var target = 0;
	var randomResult = 0;
	while(target<30){
		randomResult = 1+Math.round(Math.random() * (result-1));
		target = two * randomResult;
	}
	
	return {
	left:target,flag:"",right:two,str:one+""+two
	}
}

//100以内非整除10
function innerHundredChuWithNum(){
	var one , two;
	//two = 2+Math.round(Math.random() * 7);
	//one = 50 + Math.round(Math.random() * 49);
	
	var target = 0;
	while(target == 0 ){
		two = 2+Math.round(Math.random() * 7);
		one = 50 + Math.round(Math.random() * 49);
		target = one % two;
	}
	
	return {
	left:one,flag:"",right:two,str:one+""+two
	}
}

//500以内非整除10
function innerFiveHundred(){
	var one , two;
	
	var target = 0;
	while(target == 0 ){
		two = 10+Math.round(Math.random() * 9);
		one = 250 + Math.round(Math.random() * 249);
		target = one % two;
	}
	
	return {
	left:one,flag:"",right:two,str:one+""+two
	}
}

</script>
<style>
body{overflow-x:hidden;overflow-y:auto;}
.chu{
	background-image:url('./chu.png');
	background-repeat :no-repeat;
	margin:0px;
	padding:0px;
}
</style>

<body>
<div id="testList">

<!--
<table class="testTable" width="100%">
<tr class="testTableRow">
	<td>
		<table class="shuTestItem" border="0" width="150px">
			<tr>
				<td width="20%"></td>
				<td width="14px"></td>
				<td></td>
			</tr>
			<tr>
				<td align="right" class="fontClass">23</td>
				<td style="background-image:url('./chu.png');background-repeat :no-repeat ;margin:0px;padding:0px"></td>
				<td align="left" class="fontClass"><div class="line">36</div></td>
			</tr>
			<tr height="50px"></tr>
		</table>
	</td>
	<td>
		<table class="shuTestItem" border="0" width="150px">
			<tr>
				<td width="20%"></td>
				<td width="14px"></td>
				<td></td>
			</tr>
			<tr>
				<td align="right" class="fontClass">23</td>
				<td style="background-image:url('./chu.png');background-repeat :no-repeat ;margin:0px;padding:0px"></td>
				<td align="left" class="fontClass"><div class="line">36</div></td>
			</tr>
			<tr height="50px"></tr>
		</table>
	</td>
	<td>
		<table class="shuTestItem" border="0" width="150px">
			<tr height="20px"></tr>
			<tr>
				<td width="20%"></td>
				<td width="14px"></td>
				<td></td>
			</tr>
			<tr>
				<td align="right" class="fontClass">23</td>
				<td style="background-image:url('./chu.png');background-repeat :no-repeat ;margin:0px;padding:0px"></td>
				<td align="left" class="fontClass"><div class="line">36</div></td>
			</tr>
			<tr height="50px"></tr>
		</table>
	</td>
</tr>
</table>
-->
</div>
</body>
</html>